<?
	require_once('model/User.php');
	require_once('model/Question.php');
	require_once('model/Car.php');
	require_once('model/Article.php');
	

	final class ArticleController extends Controller {

		public function UnknownRoute() {
			$this->params['ERROR'] = true;
		}
		
		public function ShowEditor(){
			
			if(!CurrentUserIsAdmin()){
				$this->Redirect('/');
				return ;
			}
			if(isset($this->params['art_id'])){
			 	$id = (int)$this->params['art_id'];
			 					 	
			 					 	
			 	$article = new Article($id);
			 	$article->Load();
			 	$this->styleparams['title'] = $article->Title();
			 	$cat = $article->Category();
			 	$this->styleparams['category'] = $cat->Id();
			 	$this->styleparams['link'] = $article->Link();
			 	$this->styleparams['content'] = $article->Content();
		 		$this->styleparams['art_id'] = $id;
				$_SESSION['user_files_upload_path']='/media/article/'.$id.'/';
				
			}
			else{
				
				$_SESSION['user_files_upload_path']='/fcktemp/';	
				clear_dir($_SERVER['DOCUMENT_ROOT'].'/fcktemp');
				
			}
			
			$this->stylesheet = 'edit-article';
			$this->page->MountSource(Category::AllParentCategoryes());
			
		
			
		}
		
		
		
		public function Check() {
			//print_r($_FILES);
			
			
			if(!CurrentUserIsAdmin()){
				$this->Redirect('/');
				return ;
			}

			
			$art_id = (int)$this->request['art_id'];
			if($art_id<1) $obj = new Article();
			else{
				$obj = new Article($art_id);
				$obj->Load();				
		
			}
			$obj->Title(mes($this->request['title']));
			$obj->Content(CutRN($this->request['editor']));
			$cat = new Category($this->request['category']);
			$cat->Load();
			$obj->Category($cat);
			$obj->Link($this->request['link']);
			$obj->Save();
			if($art_id<1){
				copy_dir2($_SERVER['DOCUMENT_ROOT'].'/fcktemp', $_SERVER['DOCUMENT_ROOT'].'/media/article/'.$obj->Id());
				
				$obj->Content(str_replace('/fcktemp/','/media/article/'.$obj->Id().'/', $obj->Content()));
				
				$obj->Save();
			}
			
			$this->Redirect('/');

		}			
		
		public function Remove(){
			if(!CurrentUserIsAdmin()){
				$this->Redirect('/');
				return ;
			}
			$art_id = (int)$this->params['art_id'];
			$obj = new Article($art_id);			
			$obj->Remove();
			$this->Redirect('/');
			
			$art_dir = $_SERVER['DOCUMENT_ROOT'].'/media/article/'.$art_id;
			if(is_dir($art_dir))
				del_dir($art_dir);
		}
		
		public function ShowArticle(){
			$this->stylesheet = 'Article';
			$obj = new Article();
			$obj->Link($this->params['article_link']);
			$obj->Load();
			$this->page->MountSource($obj->AsSource());
		}

	}
?>